/******************************************************************************

    Simple Authentication and Authorisation System (SAAS)

    log.c

    Copyright (C) 2011  Josh Goes


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    If you require a mailing address for this software, please send your
    mail to the following address:
        SAAS
        GPO Box 2973
        BRISBANE QLD 4001
        AUSTRALIA

******************************************************************************/


#include "saas-build.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <security/pam_ext.h>


void logh( saas_t *inst , uint8_t level , const saas_host_t *host , const char *pattern , ... )
{
	va_list args;

	// if the log message is less of a priority than what we want, ignore it.
	if ( level > inst->log_level )
		return;

	switch ( inst->log_type )
	{

		case SAAS_LOG_NONE:
			// do nothing with the message.
			break;

		case SAAS_LOG_STDOUT:
			va_start( args , pattern );
			vprintf( pattern , args );
			va_end( args );
			if ( host->type == SAAS_HOST_TYPE_DNS )
				printf( "%s:%d (DNS)" , host->dns_name , host->port );
			if ( host->type == SAAS_HOST_TYPE_IPV4 )
				printf( "%d.%d.%d.%d:%d (IPv4)" ,
					host->ipv4_addr[0] ,
					host->ipv4_addr[1] ,
					host->ipv4_addr[2] ,
					host->ipv4_addr[3] ,
					host->port );
			if ( host->type == SAAS_HOST_TYPE_IPV6 )
				printf( "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d" ,
					host->ipv6_addr[ 0] ,
					host->ipv6_addr[ 1] ,
					host->ipv6_addr[ 2] ,
					host->ipv6_addr[ 3] ,
					host->ipv6_addr[ 4] ,
					host->ipv6_addr[ 5] ,
					host->ipv6_addr[ 6] ,
					host->ipv6_addr[ 7] ,
					host->ipv6_addr[ 8] ,
					host->ipv6_addr[ 9] ,
					host->ipv6_addr[10] ,
					host->ipv6_addr[11] ,
					host->ipv6_addr[12] ,
					host->ipv6_addr[13] ,
					host->ipv6_addr[14] ,
					host->ipv6_addr[15] ,
					host->port );
			printf( "\n" );
			break;

		case SAAS_LOG_SYSLOG:
			break;

		case SAAS_LOG_FILE:
			break;

	}
}

void logm( saas_t *inst , uint8_t level , const char *pattern , ... )
{
	va_list args;

	// if the log message is less of a priority than what we want, ignore it.
	if ( level > inst->log_level )
		return;

	switch ( inst->log_type )
	{

		case SAAS_LOG_NONE:
			// do nothing with the message.
			break;

		case SAAS_LOG_STDOUT:
			va_start( args , pattern );
			vprintf( pattern , args );
			va_end( args );
			printf( "\n" );
			break;

		case SAAS_LOG_SYSLOG:
			break;

		case SAAS_LOG_FILE:
			break;

		case SAAS_LOG_PAM_SYSLOG:
			va_start( args , pattern );
			pam_vsyslog( inst->log_handle , level , pattern , args );
			va_end( args );
			break;

	}
}


void logd( saas_t *inst , uint8_t level , const void *data , uint32_t data_len )
{
	uint8_t *d = (uint8_t *) data;

	// if the log message is less of a priority than what we want, ignore it.
	if ( level > inst->log_level )
		return;

	switch ( inst->log_type )
	{

		case SAAS_LOG_NONE:
			// do nothing with the message.
			break;

		case SAAS_LOG_STDOUT:
			while ( data_len-- > 0 )
				printf( "%02x " , *(d++) );
			printf( "\n" );
			break;

		case SAAS_LOG_SYSLOG:
			break;

		case SAAS_LOG_FILE:
			break;

		case SAAS_LOG_PAM_SYSLOG:
			break;
	}
}



